﻿//交易类型更变的时候同事选择价格
function SelectTradetype() {
    var type = document.getElementById("tradetype").value;
    if (type == "求租") {
        document.getElementById("priceUnit").value = "元";
    } else {
        document.getElementById("priceUnit").value = "万元";
    }
}

$(document).ready(function() {
    var cid = LexunJS.GetQueryString("id");

    var typeid = LexunJS.GetQueryString("typeid"); // 2 表示转介客预登
    if (typeid == 2) {
        $("#hTypeID").val("2");
        $("#tr_change").show();
    }

    if (cid > 0) {
        document.getElementById("cid").value = cid;
        document.getElementById("title").innerText = "修改客源";
        document.getElementById("divRest").style.display = "none";
        document.getElementById("divReturn").style.display = "";

        showDetail(cid);
    } else {
    document.getElementById("title").innerText = "客源录入";
     }
});

//客源详细信息
function CheckTel() {
    var strTel = document.getElementById("customerTel").value;
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=GetCustTelList&CustTel=' + strTel,
        timeout: '10000',
        error: function() {
            alert("系统繁忙，请稍候在试！");
        },
        success: function(json) {
            CheckTelDetailCallBack(json);
        }
    });
}

function CheckTelDetailCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) return;
    var recordLength = record.length;
    var str = "此电话已在以下部门存在：\n\n";
    for (var i = 0; i < recordLength; i++) {
        str += "部门：" + record[i].ownerdept + ",  时间：" + record[i].ownerdate + "; \n";
    }
    alert(str);
}

//客源详细信息
function showDetail(id) {
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=customer_detail&id=' + id,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        },
        success: function(json) {
            showDetailCallBack(json);
        }
    });
}

function showDetailCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;
    if (record == null || record.length == 0) return;
    var recordLength = record.length;

    if (record[0].mark1 == "1") {
        alert("待审客源，不允许修改！");
        self.parent.closeTab('修改客源');
        return;
    }
    
    for (var i = 0; i < recordLength; i++) {
        document.getElementById("customer").value = record[i].customer;
        document.getElementById("customerTel").value = record[i].customertel;
        document.getElementById("occupation").value = record[i].occupation;
        document.getElementById("agent").value = record[i].agent;
        document.getElementById("agentTel").value = record[i].agenttel;
        document.getElementById("remark").value = record[i].remark;
        if (record[i].minfloor == 0) {
            document.getElementById("minFloor").value = "";
        } else {
            document.getElementById("minFloor").value = record[i].minfloor;
        }

        if (record[i].maxfloor == 0) {
            document.getElementById("maxFloor").value = "";

        } else {
            document.getElementById("maxFloor").value = record[i].maxfloor;
        }

        if (record[i].minprice == 0) {
            document.getElementById("minPrice").value = "";
        } else {
            document.getElementById("minPrice").value = record[i].minprice;
        }

        if (record[i].maxprice == 0) {
            document.getElementById("maxPrice").value = "";
        } else {
            document.getElementById("maxPrice").value = record[i].maxprice;
        }

        document.getElementById("priceUnit").value = record[i].priceUnit; //价格单位
        if (record[i].minacreage == 0) {
            document.getElementById("minAcreage").value = "";
        } else {
            document.getElementById("minAcreage").value = record[i].minacreage;
        }

        if (record[i].maxacreage == 0) {
            document.getElementById("maxAcreage").value = "";
        } else {
            document.getElementById("maxAcreage").value = record[i].maxacreage;
        }

        if (record[i].room == 0) {
            document.getElementById("room").value = "";
        } else {
            document.getElementById("room").value = record[i].room;
        }
        
        if (record[i].room1 == 0) {
            document.getElementById("room1").value = "";
        } else {
            document.getElementById("room1").value = record[i].room1;
        }
        
        document.getElementById("markname").value = record[i].owner;
        document.getElementById("markdept").value = record[i].ownerdept;
        document.getElementById("occupation").value = record[i].occupation;
        document.getElementById("customerClass").value = record[i].customerclass;
        document.getElementById("infoFrom").value = record[i].infofrom;
        document.getElementById("tradetype").value = record[i].tradetype;
        document.getElementById("status").value = record[i].status;
        document.getElementById("payType").value = record[i].paytype;
        document.getElementById("type").value = record[i].type;
        document.getElementById("usage").value = record[i].usage;
        document.getElementById("dname").value = record[i].dname;
        document.getElementById("did").value = record[i].did;
        document.getElementById("areaname").value = record[i].area;
        document.getElementById("areaid").value = record[i].aid;
        document.getElementById("hEstateName").value = record[i].estates;
        document.getElementById("houseStyle").value = record[i].housestyle;
        document.getElementById("Direct").value = record[i].direct;
        document.getElementById("fitment").value = record[i].fitment;
        document.getElementById("iEstateName").value = record[i].estates; //楼盘
        document.getElementById("buyingMotive").value = record[i].buyingmotive; //购买动机
    }
}
var checkSubmitFlg = false;

function submitOk() {

    
    var usage = document.getElementById("usage").value;
    if (usage == "请选择") {
        alert("请选择用途！");
        return false;
    }
    var customer = document.getElementById("customer").value;
    if (customer == "") {
        alert("请输入客户名称！");
        return false;
    }
    var customerTel = document.getElementById("customerTel").value;
    if (customerTel == "") {
        alert("请输入客户电话！");
        return false;
    } else {
        var p1 = /^((1[0-9]{1}[0-9]{1})+\d{8})|(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)$/;
        if (customerTel.substr(0, 1) == "1") {
            if (customerTel.length != "11") {
                alert("号码格式不正确，请检查输入的电话号码!");
                return false;
            }
        }
        if (p1.test(customerTel) == false && customerTel != "") {
            alert("号码格式不正确，请检查输入的电话号码!");
            return false;
        }
    }
    var infoFrom = document.getElementById("infoFrom").value;

    if (infoFrom == "请选择") {
        alert("请选择来源！");
        return false;
    }

    var tradetype = document.getElementById("tradetype").value;
    if (tradetype == "求购") {
        var buyingMotive = document.getElementById("buyingMotive").value;
        if (buyingMotive == "请选择") {
            alert("请选择购买动机！");
            return false;
        }
    }

    if (tradetype == "请选择") {
        alert("请选择交易类型！");
        return false;
    }
    var agentTel = document.getElementById("agentTel").value;
    var p1 = /^((1[0-9]{1}[0-9]{1})+\d{8})|(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)$/;
    if (agentTel.substr(0, 1) == "1") {
        if (agentTel.length != "11") {
            alert("号码格式不正确，请检查输入的电话号码!");
            return false;
        }
    }
    if (p1.test(agentTel) == false && agentTel != "") {
        alert("号码格式不正确，请检查输入的电话号码!");
        return false;
    }
    var agent = document.getElementById("agent").value;
    if (agentTel != "" && agent == "") {
        alert("请输入代理人姓名！");
        return false;
    }
    var num = document.getElementById("minFloor").value;
    if (isNaN(num)) {
        alert("请输入数字！");
        return false;
    }

    var num1 = document.getElementById("maxFloor").value;
    if (isNaN(num1)) {
        alert("请输入数字！");
        return false;
    }

    var typeid = $("#hTypeID").val(); // 2 表示转介客预登
    if (typeid == 2) {
        var customerName = $("#usubclass").val();

        if (customerName == "") {
            alert("接收人不能为空！");
            document.getElementById("usubclass").focus();
            return false;
        }
    }

    if (!checkSubmitFlg) {

        // 第一次提交
        checkSubmitFlg = true;
    } else {

        //重复提交
        alert("按一次就够了，请勿重复提交！请耐心等待！谢谢合作！");
        return false;
    }

    
    document.forms[0].submit();
}

function IsNum(num) {
    if (isNaN(num.value)) {
        alert("请输入数字！");
        num.value = "";
        return false;
    }
}

function ShowDialog(url) {
    var idlist = document.getElementById("did").value;
    var path = url + "?CityId=" + idlist;
    var parameter = "城区片区";
    var style = "dialogWidth:600px; dialogHeight:470px; status:0;scroll:1; help:0; resizable:1;Scrollbars=yes,Toolbar=no,Location=no,Direction=no,Resizeable=no";
    var returnValue = window.showModalDialog(path, parameter, style);
    if (returnValue == "" || typeof (returnValue) == "undefined") {
        return;
    } else {
        var str = new Array();
        str = returnValue.split('|');
        document.getElementById("areaname").value = str[1];
        document.getElementById("areaid").value = str[0];
    }
}

function SelectValue() {
    var name = LexunJS.GetSelectValue("sDirect");
    if (name == "") {
        document.getElementById("Direct").value = "";
        return;
    }
    var DirectName = document.getElementById("Direct");
    if (DirectName.value == "") {
        DirectName.value = name;
    } else {
        DirectName.value += "," + name;
    }
}

function loadCity() {
    var id = LexunJS.GetSelectValue("Sdame"); //id
    var name = LexunJS.GetSelectText("Sdame"); //名称
    if (name == "全部") {
        document.getElementById("dname").value = "";
        document.getElementById("did").value = "";
        return;
    }
    var cityname = document.getElementById("dname");
    var cityid = document.getElementById("did");
    if (cityname.value == "") {
        cityname.value = name;
        cityid.value = id;
    } else {
        var cityList = cityname.value; //验证是否存在相同的片区,如果存在则不添加
        var strs = new Array(); //定义一数组
        strs = cityList.split(","); //字符分割
        for (var i = 0; i < strs.length; i++) {
            if (name == strs[i]) {
                name = "";
                id = "";
            }
        }
        if (name != "" && id != "") {
            cityname.value += "," + name;
            cityid.value += "," + id;
        }
    }
}

function DivDisplay() {
    var usage = document.getElementById("usage").value;
    var divhx1 = document.getElementById("divhx1");
    var divStyle = document.getElementById("divStyle");
    var divfitment = document.getElementById("divFitment");

    if (usage == "商铺") {
        divhx1.style.display = "none";
        divStyle.style.display = "none";
        divfitment.style.display = "none";
    } else {

        divhx1.style.display = "";
        divStyle.style.display = "";
        divfitment.style.display = "";
    }
}

//验证电话号码以及手机号码
function chkForm(txtOTel) {
    var p1 = /^((1[0-9]{1}[0-9]{1})+\d{8})|(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^([0-9]{3,4}\-)?([0-9]{3,4})?(\-[0-9]{3,4}))$/;
    
    if (txtOTel.value.substr(0, 1) == "1") {
        if (txtOTel.value.length != "11") {
            alert("号码格式不正确，请检查输入的电话号码");
            txtOTel.value = "";
            return false;
        }
    }

    if (p1.test(txtOTel.value) == false && txtOTel.value != "") {
        alert("号码格式不正确，请检查输入的电话号码");
        txtOTel.value = "";
        return false;
    }
}

//部门
$("#umainclass").autocomplete("../Ajax/GetDeptAuto.aspx", { minChars: 2, extraParams: { dpname: function() { return escape($('#umainclass').val()); } }, scrollHeight: 200, width: 150, max: 100, parse: function(data) {
    if (data == "" || data == undefined)
        return false;

    return $.map(eval(data), function(row) {
        return {
            data: row,
            value: row.id,
            result: row.name
        }
    });
},
    formatItem: function(item) {
        return item.name;
    }

});
$("#umainclass").result(function(event, data, formatted) {
    if (data == null) {
        return;
    }
    // 选中下拉中某行的事件
    document.getElementById("utxtmain").value = data.name;
});
//返回
function ReBack() {
    self.parent.closeTab('修改客源');
 }